package com.woniuxy.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.QuestionOption;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Test;
import com.woniuxy.entity.User;
import com.woniuxy.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author ylr
 * @time 2022/05/30 20:51:58
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    IUserService userService;

    @RequestMapping("login")
    public ResponseEntity login(@RequestBody User user){
        return userService.login(user.getUsername(), user.getPassword());
    }
    @RequestMapping ("/userList/{currentPage}/{pageSize}")
    @RequiresPermissions("system")
    public ResponseEntity findUser(@PathVariable int currentPage, @PathVariable int pageSize, @RequestBody String jsonString) {
        Map<String, Object> map = new HashMap<>();
        //将json字符串转换为json对象
        JSONObject json = JSONObject.parseObject(jsonString);
        //将json对象循环转换为map
        Iterator it = json.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
            if (entry.getValue().toString().length() == 0){
                map.put(entry.getKey(), null);
            }else {
                map.put(entry.getKey(), entry.getValue());
            }
        }
        System.out.println("打印map"+map);
        //System.out.println(currentPage+"分隔符"+pageSize);
        PageHelper.startPage(currentPage, pageSize);
        List<Test> all = userService.findUsers(map);
        //添加分页信息
        PageInfo<Test> testPageInfo = new PageInfo<>(all);
        return new ResponseEntity(testPageInfo);
    }


    @RequestMapping ("addUser")
    @RequiresPermissions("system")
    public ResponseEntity addUser(@RequestBody Map map){



        return userService.addUser(map);
    }
}
